package com.murphy.algorithm.likou;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dongsufeng
 * @version 4.0
 * @date 2023/2/23 11:15
 */
public class ValiTree {

    static List<Integer> list = new ArrayList<>();

    public void valid(TreeNode root) {
        if (root==null){
            return;
        }
        //按照左、中、右的顺序放入list，判断list是否是有序的即可
        valid(root.l);
        list.add(root.val);
        valid(root.r);

    }


    public static class TreeNode {
        private int val;
        private TreeNode l;
        private TreeNode r;

        public TreeNode() {
        }

        public TreeNode(int val) {
            this.val = val;
        }
    }

    public static void main(String[] args) {
        LowestCommonAncestor lowestCommonAncestor = new LowestCommonAncestor();
        TreeNode treeNode = new TreeNode(5);
        TreeNode treeNode3 = new TreeNode(3);
        treeNode.l = treeNode3;
        TreeNode treeNode2 = new TreeNode(2);
        treeNode.l.l = treeNode2;
        TreeNode treeNode4 = new TreeNode(4);
        treeNode.l.r = treeNode4;

        TreeNode treeNode7 = new TreeNode(7);
        treeNode.r = treeNode7;
        TreeNode treeNode6 = new TreeNode(6);
        treeNode.r.l = treeNode6;
        TreeNode treeNode8 = new TreeNode(8);
        treeNode.r.r = treeNode8;

        ValiTree valiTree = new ValiTree();
        valiTree.valid(treeNode);

        System.out.println(JSON.toJSON(list));
        for (int i = 0; i < list.size()-1; i++) {
            if (list.get(i)>list.get(i+1)){
                System.out.println(false);
                return;
            }
        }
        System.out.println(true);

    }
}
